package com.tredreams.idea.counter;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;

import java.util.ArrayList;


public class Accountdatabase {
	
	   private Integer numberOfIdeas = 0;
	   private static final String DATABASE_NAME = "Idea.db";
	   private static final int DATABASE_VERSION = 1;
	   private static final String TABLE_NAME = "ideaTable";

	   private Context context;
	   private SQLiteDatabase db;

	   private SQLiteStatement insertStmt;
	   private static final String INSERT = "insert into " 
	      + TABLE_NAME + "(newIdea) values (?)";

	   public Accountdatabase(Context context) {
	      this.context = context;
	      OpenHelper openHelper = new OpenHelper(this.context);
	     
	      this.db = openHelper.getWritableDatabase();
	   
	      this.insertStmt = this.db.compileStatement(INSERT);
	   }

	   public long insert(String idea ) {
		 
		  this.insertStmt.bindString(1, idea);
	      
	      return this.insertStmt.executeInsert();
	   }
	   
	   public Integer findNumberOfIdeas()
	   {
    	 return numberOfIdeas;
	   }
	   
	   public void deleteChild(int rowId) {
		  this.db.delete(TABLE_NAME, "id=" + rowId, null); 
	   }
	   
	   public void deleteIdeaByRowid(int id) {
			  this.db.delete(TABLE_NAME, "id=" + id, null); 
		   }
	   
	   public void deleteIdeaByName(String name) {
			  
			  Cursor cursor1 = this.db.query(TABLE_NAME, new String[] {"id"},"newIdea"+"=?", new String[] {name},null,null,null);
			  if(cursor1.moveToFirst())
				{do  {this.db.delete(TABLE_NAME, "id="+Integer.parseInt(cursor1.getString(0)), null); }while(cursor1.moveToNext());}
			  cursor1.close();
		   }
	   public void deleteAll() {
		      this.db.delete(TABLE_NAME,null,null); 
		   }
       
       public ArrayList<String> ideaList()
       {
    	   numberOfIdeas = 0;
    	   ArrayList<String> namesArray = new ArrayList<String>();
    	
    	   Cursor cursor = this.db.query(TABLE_NAME, new String[] {"newIdea"}, 
   		        null, null, null, null, null);
   		      if (cursor.moveToFirst()) {
   		         do { 
   		        		namesArray.add(cursor.getString(0));
   		        		numberOfIdeas++;
   		        	 }while(cursor.moveToNext());      
   		         }
   		   Log.v("ideas", namesArray.toString());
   		   cursor.close();
    	   return namesArray;
       }
      
	   public void close() {
	        db.close();
	    }

		   private static class OpenHelper extends SQLiteOpenHelper {

		      OpenHelper(Context context) {
		         super(context, DATABASE_NAME, null, DATABASE_VERSION);
		      }

		      @Override
		      public void onCreate(SQLiteDatabase db) {
		         db.execSQL("CREATE TABLE "+TABLE_NAME+" (ID  INTEGER PRIMARY KEY Autoincrement, "+ "newIdea  TEXT);");
		      }

		      @Override
		      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		         Log.w("Accountdatabase", "Upgrading database, this will drop tables and recreate.");
		         db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
		         onCreate(db);
		      }
		   }

}

